package com.zhiwei.most.dialog

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentActivity
import androidx.viewbinding.ViewBinding
import com.blankj.utilcode.util.ActivityUtils
import com.drake.net.utils.runMain
import com.zhiwei.most.R
import com.zhiwei.most.ext.inflateBindingWithGeneric

/**
 * Create by zhiweizhu on 2022/3/4
 */
abstract class BaseDialogFragment<VB: ViewBinding>(context: Context): DialogFragment() {

    protected var mActivity: FragmentActivity? = null

    //该类绑定的 ViewBinding
    private var _binding: VB? = null
    val mViewBind: VB get() = _binding!!

    init {
        mActivity = getFragmentActivity(context)
    }

    override fun getTheme(): Int {
        val theme = onBindTheme()
        return when {
            theme != View.NO_ID -> theme
            else -> super.getTheme()
        }
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)
        dialog.run {
            setCancelable(true)
            setCanceledOnTouchOutside(true)
        }
        return dialog
    }


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding  = inflateBindingWithGeneric(inflater,container,false)
        return mViewBind.root
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initData(view, savedInstanceState)
    }


    override fun onCancel(dialog: DialogInterface) {
        super.onCancel(dialog)
        onDialogCancel(dialog)
    }

    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
        onDialogDismiss(dialog)
    }


    fun showDialog(tag: String = javaClass.simpleName){
        runMain {
            mActivity?.let {
                if (ActivityUtils.isActivityAlive(it)){
                    val fm = it.supportFragmentManager
                    val prev = fm.findFragmentByTag(tag)
                    if (prev != null){
                        fm.beginTransaction().remove(prev)
                    }
                    super.show(fm, tag)
                }
            }
        }
    }



    override fun dismiss() {
        runMain {
            mActivity?.let {
                if (ActivityUtils.isActivityAlive(it)){
                    super.dismissAllowingStateLoss()
                }
            } ?: super.dismiss()
        }
    }


    private fun getFragmentActivity(context: Context): FragmentActivity? {
        val activity = ActivityUtils.getActivityByContext(context) ?: return null
        if (activity is FragmentActivity) {
            return activity
        }
        return null
    }


    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }


    open fun onBindTheme(): Int = R.style.CommonContentDialogStyle

    abstract fun initData(view: View, savedInstanceState: Bundle?)

    abstract fun onDialogCancel(dialog: DialogInterface)

    abstract fun onDialogDismiss(dialog: DialogInterface)





}